<template>
  <view class="container">

    <view class="repair-form">
      <view class="form-item">
        <span>锁上二维码：</span>
        <input class="code-input" type="text" placeholder="请输入锁上二维码" v-model="newCode" />
        <view class="scan-icon" @click="scanCode('new')">
          <u-icon name="scan" size="48" color="#74b9ff"></u-icon>
        </view>
      </view>
      <view class="form-item">
        <span>亚克力二维码：</span>
        <input class="code-input" type="text" placeholder="请输入亚克力二维码" v-model="oldCode" />
        <view class="scan-icon" @click="scanCode('old')">
          <u-icon name="scan" size="48" color="#74b9ff"></u-icon>
        </view>
      </view>

    </view>


    <div style="margin-top: 20rpx;">
      <u-button @click="getCode" text="检测" type="success">检测</u-button>
    </div>

  </view>
</template>

<script>
import { exchangeDevice } from '@/api/index';
export default {
  data() {
    return {
      newCode: "",
      oldCode: "",
    };
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) { },

  methods: {
    async getCode() {
      try {
        await exchangeDevice({
          equipment_info_id1: this.newCode,
          equipment_info_id2: this.oldCode,
        });

        uni.showToast({
          title: "完成绑定",
          icon: "success",
        });
        setTimeout(() => {
          uni.navigateBack();
        }, 1000);
      } catch (error) {

        uni.showToast({
          title: "绑定失败",
          icon: "error",
        });
      }

    },
    scanCode(type) {
      const app = this;
      uni.scanCode({
        scanType: ["qrCode", "barCode"],
        success(res) {
          console.log('res.result', res.result);

          const _code = encodeURIComponent(
            res?.result?.split("kpl/")?.[1])

          if (type === 'new') {
            app.newCode = _code
          } else {
            app.oldCode = _code
          }


        },
        fail(err) {
          if (err.errMsg.includes("cancel")) {
            return;
          }
          uni.showToast({
            title: "扫码失败，请重试",
            icon: "none",
          });
        },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  min-height: 100vh;
  background: #ebebeb;
  padding: 30rpx;
}

.card_item {
  border-radius: 18rpx;
  background-color: rgba(255, 255, 255, 1);
  padding: 20rpx 30rpx;
}

.repair-form {
  padding: 30rpx;
  background: #ffffff;


  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);

  .form-item {

    border-radius: 20rpx;

    display: flex;
    align-items: center;
    margin-bottom: 20rpx;

    .code-input {
      flex: 1;
      font-size: 28rpx;
      color: #333333;
      border: none;
      outline: none;

      &::placeholder {
        color: #cccccc;
      }
    }

    .scan-icon {
      width: 60rpx;
      height: 60rpx;
      display: flex;
      align-items: center;
      justify-content: center;
    }


    .option-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 20rpx 0;

      &:not(:last-child) {
        border-bottom: 1rpx solid #f5f5f5;
        margin-bottom: 20rpx;
      }

      .option-text {
        font-size: 28rpx;
        color: #333333;
      }

      &.active {
        .option-text {
          color: #74b9ff;
        }
      }
    }

    .description-input {
      width: 100%;
      min-height: 200rpx;
      font-size: 28rpx;
      color: #333333;
      line-height: 1.6;
      border: none;
      outline: none;
      resize: none;

      &::placeholder {
        color: #cccccc;
      }
    }
  }

  .submit-section {
    margin-top: 80rpx;

    .submit-btn {
      width: 100%;
      height: 100rpx;
      border-radius: 50rpx;
      display: flex;
      align-items: center;
      justify-content: center;
      background: #cccccc;

      .submit-text {
        font-size: 32rpx;
        font-weight: 600;
        color: #ffffff;
      }

      &:not(.disabled) {
        background: #74b9ff;
        box-shadow: 0 8rpx 24rpx rgba(116, 185, 255, 0.4);

        &:active {
          transform: translateY(2rpx);
          box-shadow: 0 6rpx 20rpx rgba(116, 185, 255, 0.5);
        }
      }
    }
  }
}
</style>
